#include "temp.h"
#include "msgd.h"


static int tempLogLevel = LOG_WARNING;

void setTempLogLevel(int logLevel)
{
    tempLogLevel = logLevel;
}

double readTemp()
{
    int fd_temp = open(TEMP_PATH,O_RDONLY);
    if(fd_temp < 0) 
    {
        perr_d(true,tempLogLevel,"function open error when called readTemp");
        return -1;
    }

    char buf[20] = {'\0',};
    int readsize = read(fd_temp, buf, 20);
    if(readsize < 0)
    {
        perr_d(true,tempLogLevel,"function read error when called readTemp");
        return -1;
    }
    close(fd_temp);

    return (atoi(buf) / 1000.0);
}

/*
int getTempLevel()
{
    double temp = readTemp();
    if (temp < 50)                      return TEMP_FREEZING;
    else if (50 <= temp && temp < 52)   return TEMP_NORMAL_LOW;
    else if (52 <= temp && temp < 54)   return TEMP_NORMAL_MIDDLE;
    else if (54 <= temp && temp < 56)   return TEMP_NORMAL_HIGH;
    else if (56 <= temp && temp < 58)   return TEMP_GREAT_HIGH;
    else if(temp >= 58)                 return TEMP_EXTREME_HIGH;
    return -1;
}

*/

int getTempLevel()
{
    double temp = readTemp();
    if (temp < 40)                      return TEMP_FREEZING;
    else if (40 <= temp && temp < 45)   return TEMP_NORMAL_LOW;
    else if (45 <= temp && temp < 50)   return TEMP_NORMAL_MIDDLE;
    else if (50 <= temp && temp < 55)   return TEMP_NORMAL_HIGH;
    else if (55 <= temp && temp < 60)   return TEMP_GREAT_HIGH;
    else if(temp >= 60)                 return TEMP_EXTREME_HIGH;
    return -1;
}
